import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/home',
    component:Home
  }
]

const router = new VueRouter({
  routes
})

//  导航的守卫  
// to   去哪里
// form  从哪来 
// next  放行的意思
router.beforeEach((to,from,next)=>{
  console.log(to.path)
  console.log(from)
  if(to.path=='/login'){
    return next()
  }
  //建议存储到 localStorage
  const token = localStorage.getItem('token')
  if(!token){
    // 进入if 判断说明不存在 
    // next   里面支持传递参数
    return next('/login') 
  }
  // 进入这个就说明token存在
  next()
})

export default router
